package com.tiancheng.trade.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.payment.mapper.PaymentChannelMapper;
import com.tiancheng.trade.payment.model.PaymentChannel;
import com.tiancheng.trade.payment.service.PaymentChannelService;
import com.tiancheng.trade.payment.util.ObjectConvertUtil;
import com.tiancheng.trade.payment.vo.admin.PaymentChannelAddVO;
import com.tiancheng.trade.payment.vo.admin.PaymentChannelReqVO;
import com.tiancheng.trade.payment.vo.api.res.PaymentChannelResVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付渠道
 * @author su
 *
 */
@Service
public class PaymentChannelServiceImpl implements PaymentChannelService {

    @Autowired
    PaymentChannelMapper paymentChannelMapper;

	/**
	 * 根据id获取支付渠道
	 * @param id
	 * @return
	 */
    public PaymentChannel get(Long id){
        return paymentChannelMapper.selectById(id);
    }

    /**
     * 获取所有支付渠道
     */
    public List<PaymentChannel> getAll(){
        QueryWrapper<PaymentChannel> conditions = new QueryWrapper<>();
        return paymentChannelMapper.selectList(conditions);
    }

    /**
     * 获取所有支付渠道(转换)
     */
    public List<PaymentChannelResVO> getAllConvert(){
        List<PaymentChannel> list = getAll();
        List<PaymentChannelResVO> listRes = list.stream().map(res ->
                ObjectConvertUtil.convert(res, PaymentChannelResVO.class).get()).collect(Collectors.toList());
        return listRes;
    }

    /**
     * 根据支付渠道编码获取支付渠道
     * @param paymentChannelCode 支付渠道编码
     * @return
     */
    public PaymentChannel getByPaymentChannelCode(String paymentChannelCode){    	
        QueryWrapper<PaymentChannel> conditions = new QueryWrapper<>();
        conditions.eq("payment_channel_code", paymentChannelCode);
        PaymentChannel paymentChannel = paymentChannelMapper.selectOne(conditions);
        return paymentChannel;
    }

    /**
     * 根据支付渠道编码获取支付渠道(转换)
     * @param paymentChannelCode
     * @return
     */
    public PaymentChannelResVO getByPaymentChannelCodeConvert(String paymentChannelCode){
        PaymentChannel paymentChannel =  getByPaymentChannelCode(paymentChannelCode);
        return ObjectConvertUtil.convert(paymentChannel, PaymentChannelResVO.class).get();
    }

    /**
     * 添加支付渠道
     * @param paymentChannelVO
     * @return
     */
    public long add(PaymentChannelAddVO paymentChannelVO) {
        PaymentChannel paymentChannelQuery = getByPaymentChannelCode(paymentChannelVO.getPaymentChannelCode());
        if(paymentChannelQuery!=null){
            BusinessErrorInfoEnum.PAYMENT_CHANNEL_ADD_EXIST_ERROR.assertFail();
        }
    	PaymentChannel paymentChannel = ObjectConvertUtil.convert(paymentChannelVO, PaymentChannel.class).get();
        int num = paymentChannelMapper.insert(paymentChannel);
        BusinessErrorInfoEnum.PAYMENT_CHANNEL_ADD_ERROR.assertIsTrue(num > 0);
        return paymentChannel.getId();
    }

    /**
     * 分页查询支付渠道
     * @param params
     * @return
     */
    public PageInfo findList(PaymentChannelReqVO params){
        int page = params.getCurrentPage();
        int limit =  params.getPageSize();
        QueryWrapper<PaymentChannel> conditions = new QueryWrapper<>();
        conditions.orderByDesc("create_time");
        PageInfo<PaymentChannel>  pageInfo = paymentChannelMapper.find(conditions, page, limit);
        PageInfo<PaymentChannelResVO> pageRes = new PageInfo<>();
        List<PaymentChannelResVO> vos = pageInfo.getList().stream().map(obj ->
                ObjectConvertUtil.convert(obj, PaymentChannelResVO.class).get()).collect(Collectors.toList());
        pageRes.setList(vos);
        pageRes.setPagination(pageInfo.getPagination());
        return pageRes;
    }
    
}
